# setwd("~/Dropbox/T&G project survey/Ukraine/replication_JOP")
library(tidyverse)
library(scales)
source("func.R")

# ggplot defaults: geoms theme
update_geom_defaults("text", list(family = "Archivo Narrow"))
update_geom_defaults("label", list(family = "Archivo Narrow"))
theme_set(theme_nice())

# Read, clean, recode
df = haven::read_sav("data/CIS/MD3355/3355.sav") %>%
  janitor::clean_names() %>%
  mutate(
    p4 = recode(as_factor(p4), "Mucho" = "A lot", "Bastante" = "Quite",
      "Algo" = "Something", "Poco" = "Little", "Nada" = "Nothing"),
    p5 = recode(as_factor(p5), "Muy preocupado/a" = "Extremely",
      "Bastante preocupado/a" = "Very", "Algo preocupado/a" = "Somewhat",
      "Poco preocupado/a" = "Little", "Nada preocupado/a" = "Not at all"),
    p6 = recode(as_factor(p6), "S<U+00ED> cree que es posible" = "Possible",
      "No cree que sea posible" = "Impossible"),
    p11 = recode(as_factor(p11), "Concierne mucho" = "Extremely",
      "Concierne bastante" = "Very", "Concierne algo" = "Somewhat",
      "Concierne poco" = "Little", "No concierne nada" = "Not at all"),
    p12_1 = recode(as_factor(p12_1), "Muchas" = "Extremely", "Bastantes" = "Very",
      "Algunas" = "Somewhat", "Pocas" = "Little", "Ninguna" = "Not at all"),
    p9_1 = recode(as_factor(p9_1), "Muy acuerdo" = "Completely agree",
      "Bastante de acuerdo" = "Agree", "Algo de acuerdo" = "Somewhat agree",
      "Poco de acuerdo" = "Disagree", "Nada de acuerdo" = "Completely disagree"),
    p9_2 = recode(as_factor(p9_2), "Muy acuerdo" = "Completely agree",
      "Bastante de acuerdo" = "Agree", "Algo de acuerdo" = "Somewhat agree",
      "Poco de acuerdo" = "Disagree", "Nada de acuerdo" = "Completely disagree"),
    p9_3 = recode(as_factor(p9_3), "Muy acuerdo" = "Completely agree",
      "Bastante de acuerdo" = "Agree", "Algo de acuerdo" = "Somewhat agree",
      "Poco de acuerdo" = "Disagree", "Nada de acuerdo" = "Completely disagree"),
    ) %>%
  mutate(escideol = ifelse(escideol %in% 98:99, NA, escideol))


# df$know = ifelse(df$p4 %in% c("A lot", "Quite", "Something"), 1, 0)
# df$know = ifelse(df$p4 %in% c("N.S.", "N.C."), NA, df$know)
# df$ideo = as.integer(df$escideol)
# df$educ = ifelse(df$estudios %in% 7:9, NA, as.integer(df$estudios))
# summary(lm(know ~ ideo + edad + factor(educ), data=df))

# --------------------------------------------------------------------
# Concern items - showing compliance

items = df %>%
  select(`How much do you know about the Russian invasion of Ukraine?` = p4,
         `How concerned are you about the Russian invasion of Ukraine?` = p5,
         `How likely is it Russia invades other countries in its old area of influence?` = p6,
         `How concerning is the invasion to Spain?` = p11,
         `How will the invasion affect economic situation of Spain?` = p12_1) %>%
  pivot_longer(everything()) %>%
  filter(!str_detect(value, paste(c("N.C.", "N.S.", "NO LEER"), collapse = "|"))) %>%
  group_by(name, value) %>%
  tally() %>%
  mutate(pct = n/sum(n))


# Plot about awareness
p = ggplot(items %>% filter(grepl("How much do you", name)), aes(y = value, x = pct)) +
  geom_col() +
  # facet_wrap(vars(name), scales = "free") +
  scale_x_continuous(labels = scales::percent) +
  labs(x = "", y = "", title = "How much do you know about the\nRussian invasion of Ukraine?")
ggsave("figures/CIS3355_awareness.pdf", height = 3, width = 5, device = cairo_pdf)

# Plot about concern
p = ggplot(items %>% filter(!grepl("How much do you", name)), aes(y = value, x = pct)) +
  geom_col() +
  facet_wrap(vars(name), scales = "free") +
  scale_x_continuous(labels = scales::percent) +
  labs(x = "", y = "")
ggsave("figures/CIS3355_concern.pdf", height = 5, width = 9, device = cairo_pdf)

# --------------------------------------------------------------------
# Plot about concern depending on level of awareness

concern_know = df %>%
  select(p4,
    `How concerned are you about the Russian invasion of Ukraine?` = p5,
    `How likely is it Russia invades other countries in its old area of influence?` = p6,
    `How concerning is the invasion to Spain?` = p11,
    `How will the invasion affect Spaniards' economic situation?` = p12_1) %>%
  pivot_longer(-p4) %>%
  filter(!str_detect(value, paste(c("N.C.", "N.S.", "NO LEER"), collapse = "|"))) %>%
  filter(!str_detect(p4, paste(c("N.C.", "N.S.", "NO LEER"), collapse = "|"))) %>%
  mutate(p4 = recode(p4,
    "A lot" = "Something/A lot", "Quite" = "Something/A lot", "Something" = "Something/A lot",
    "Little" = "Little/Nothing", "Nothing" = "Little/Nothing")) %>%
  group_by(name, p4, value) %>%
  tally() %>%
  mutate(pct = n/sum(n))

p = ggplot(concern_know, aes(y = value, x = pct, group = p4, fill = p4)) +
  geom_col(position = "dodge2") +
  facet_wrap(vars(name), scales = "free") +
  scale_x_continuous(labels = scales::percent) +
  labs(x = "", y = "", fill = "Knowledge about Russian invasion of Ukraine") +
  theme(legend.position = "bottom")
ggsave("figures/CIS3355_concern_cond.pdf", height = 5, width = 9, device = cairo_pdf)

# --------------------------------------------------------------------
# Institutions evaluation

institutions = df %>%
  select(p4, escideol,
    `Spanish Government` = p9_1,
    `European Union` = p9_2,
    `NATO` = p9_3) %>%
  pivot_longer(-c(p4, escideol)) %>%
  mutate(leaning = case_when(
    escideol > 5 ~ "Right", escideol <= 5 ~ "Left", TRUE ~ NA_character_)) %>%
  filter(!str_detect(value, paste(c("N.C.", "N.S.", "NO LEER"), collapse = "|"))) %>%
  filter(!str_detect(p4, paste(c("N.C.", "N.S.", "NO LEER"), collapse = "|"))) %>%
  mutate(p4 = recode(p4,
    "A lot" = "Something/A lot", "Quite" = "Something/A lot", "Something" = "Something/A lot",
    "Little" = "Little/Nothing", "Nothing" = "Little/Nothing"))

inst_all = institutions %>%
  group_by(name, value) %>%
  tally() %>%
  mutate(pct = n/sum(n))

inst_know = institutions %>%
  group_by(name, p4, value) %>%
  tally() %>%
  mutate(pct = n/sum(n))

inst_ideo = institutions %>%
  filter(!is.na(leaning)) %>%
  group_by(name, leaning, value) %>%
  tally() %>%
  mutate(pct = n/sum(n))

p = ggplot(inst_all, aes(y = value, x = pct)) +
  geom_col() +
  facet_wrap(vars(name), scales = "free", ncol=3) +
  scale_x_continuous(labels = scales::percent) +
  scale_y_discrete(labels = label_wrap(10)) +
  labs(x = "", y = "") + coord_flip()
ggsave("figures/CIS3355_instit.pdf", height = 5, width = 10, device = cairo_pdf)

p = ggplot(inst_know, aes(y = value, x = pct, group = p4, fill = p4)) +
  geom_col(position = "dodge2") +
  facet_wrap(vars(name), scales = "free", ncol=3) +
  scale_x_continuous(labels = scales::percent) +
  scale_y_discrete(labels = label_wrap(10)) +
  labs(x = "", y = "", fill = "Knowledge about Russian invasion of Ukraine") +
  theme(legend.position = "bottom") + coord_flip()
ggsave("figures/CIS3355_instit_know.pdf", height = 5, width = 10, device = cairo_pdf)

p = ggplot(inst_ideo, aes(y = value, x = pct, group = leaning, fill = leaning)) +
  geom_col(position = "dodge2") +
  facet_wrap(vars(name), scales = "free", ncol=3) +
  scale_x_continuous(labels = scales::percent) +
  scale_y_discrete(labels = label_wrap(10)) +
  labs(x = "", y = "", fill = "Ideological leaning") +
  theme(legend.position = "bottom") + coord_flip()
ggsave("figures/CIS3355_instit_ideo.pdf", height = 5, width = 10, device = cairo_pdf)
